
/*
C Language - Operator definitions
---------------------------------
exp1 ? exp2 : exp3  <-> if exp1 != 0 return exp2 else return exp3
*/


/*
PS2 Data Types - From "Playstation2 Basics - Welcome to the Machine" - Version 1.0, 2002/03/06 - Tony Saveski (dreamtime), t_saveski@yahoo.com
----------------------------------------------------------------------------------------------------------------------------------------------
Name			Asm			Bits	Bytes	Data type							Alias
---------		---			----	-----	---------							-----
Byte			.byte		8		1		char								int8 
							8		1		unsigned char						uint8, u8
                        	                                					
Half Word		.half		16		2		short								int16 
											unsigned short						uint16, u16
                        	                                					
Word			.word		32		4		int									int32 
											unsigned int						uint32, u32
                        	                                					
Double Word		.dword		64		8		long								int64
											unsigned long						uint64, u64
                        	
Quad Word					128		16

Filler			.space	[n]	8*[n]	[n]		It could be used as any type
											(a comment should indicate which)	

*/

/* Using system calls (from http://hitmen.c02.at/html/psx_faq.html)
To use system-calls from c, you need to write yourself a wrapper function that passes the arguments from/to the called bios-routine.
When an argument is required in a system call, it is always passed by $a0, $a1, $a2, and $a3, and five or more arguments are passed by $sp+0x10.
Also, the system call number is put into $t1 (), and the address (0xa0, 0xb0, 0xc0) of a system call is called. return value (if any), is returned by $v0 ().
The return format is the same as that of function call agreement of mips-gcc, so an argument (state of $a0,$a1,$a2,$a3 and a stack) is left intact, 
and $t1 () is set up. */

/*
-------------------------------------------------------------------------
ee-syscalls (http://www.oopo.net/users/danpeori/releases/ee-syscalls.txt)
-------------------------------------------------------------------------
0x02: SetGsCrt
-------------------------------------------------------------------------
This syscall is used to reset the GS and set a video mode
from a list of pre-defined modes. Other modes can be set
manually via registers, but its safer to use these modes.
-------------------------------------------------------------------------
USAGE: void SetGsCrt(interlace, mode, field)
-------------------------------------------------------------------------
ARGUMENTS:

interlace: 0x00- Non-interlaced
           0x01- Interlaced

mode:      0x02- NTSC          ( 640 x  448 @ 59.940Hz)
           0x03- PAL           ( 640 x  512 @ 50.000Hz)
          
           0x1A- VGA-640@60    ( 640 x  480 @ 59.940Hz)
           0x1B- VGA-640@72    ( 640 x  480 @ 72.809Hz)
           0x1C- VGA-640@75    ( 640 x  480 @ 75.000Hz)
           0x1D- VGA-640@85    ( 640 x  480 @ 85.008Hz)
           0x2A- VGA-800@56    ( 800 x  600 @ 56.250Hz)
           0x2B- VGA-800@60    ( 800 x  600 @ 60.317Hz)
           0x2C- VGA-800@72    ( 800 x  600 @ 72.188Hz)
           0x2D- VGA-800@75    ( 800 x  600 @ 75.000Hz)
           0x2E- VGA-800@85    ( 800 x  600 @ 85.061Hz)
           0x3B- VGA-1024@60   (1024 x  768 @ 60.004Hz)
           0x3C- VGA-1024@70   (1024 x  768 @ 70.069Hz)
           0x3D- VGA-1024@75   (1024 x  768 @ 75.029Hz)
           0x3E- VGA-1024@85   (1024 x  768 @ 84.997Hz)
           0x4A- VGA-1280@60   (1280 x 1024 @ 60.020Hz)
           0x4B- VGA-1280@75   (1280 x 1024 @ 75.025Hz)
          
           0x50- HDTV-480P     ( 720 x  480)
           0x51- HDTV-1080I    (1920 x 1080)
           0x52- HDTV-720P     (1280 x  720)
          
           0x72- DVDNTSC       ( 640 x  448 @ ??.???Hz)
           0x73- DVDPAL        ( 720 x  480 @ ??.???Hz)
           0x73- DVD480P       ( 720 x  480 @ ??.???Hz)

 field: 0x00- Field mode (Read Every Other Line)
        0x01- Frame mode (Read Every Line)
-------------------------------------------------------------------------
*/

/*
Display frequency x Typical Resolutions supported by GS
(free adaption from GS User's Manual and other sources)

+----------+-----------------------+-----------------------------------------------+
|          |                       |                Frequencies for                |
|          |                       +-----------------------+-----------------------+
|Mode      |Resolution             |Vertical   - Hz        |Horizontal - kHz       |
|          |Progressive(Interlaced)|Progressive(Interlaced)|Progressive(Interlaced)|
+----------+-----------------------+-----------------------+-----------------------+
|NTSC*     |256x448(224)           |59.940(59.82)          |15.734                 |
|          |320x224(448)           |                       |                       |
|          |384x224(448)           |                       |                       |
|          |512x224(448)           |                       |                       |
|          |640x224(448)           |                       |                       |
+----------+-----------------------+-----------------------+-----------------------+
|PAL*      |256x512(256)           |50.000(49.76)          |15.625                 |
|          |320x512(256)           |                       |                       |
|          |384x512(256)           |                       |                       |
|          |512x512(256)           |                       |                       |
|          |640x512(256)           |                       |                       |
+----------+-----------------------+-----------------------+-----------------------+
|VESA      |640x480                |59.940                 |31.349                 |
|          |                       |72.809                 |37.861                 |
|          |                       |75.000                 |37.500                 |
|          |                       |85.008                 |43.269                 |
|          +-----------------------+-----------------------+-----------------------+
|          |800x600                |56.250                 |35.156                 |
|          |                       |60.317                 |37.879                 |
|          |                       |72.188                 |48.077                 |
|          |                       |75.000                 |46.875                 |
|          |                       |85.061                 |53.674                 |
|          +-----------------------+-----------------------+-----------------------+
|          |1024x768               |60.004                 |48.363                 |
|          |                       |70.069                 |56.476                 |
|          |                       |75.029                 |60.023                 |
|          |                       |84.997                 |68.677                 |
|          +-----------------------+-----------------------+-----------------------+
|          |1280x1024              |60.020                 |63.981                 |
|          |                       |75.025                 |79.976                 |
+----------+-----------------------+-----------------------+-----------------------+
|HDTV-480p |720x480                |59.94                  |31.469                 |
+----------+-----------------------+-----------------------+-----------------------+
|HDTV-1080i|1920x1080              |60.00                  |33.750                 |
+----------+-----------------------+-----------------------------------------------+
*/

/*
---------------------------------------------------------------------------------------------------------
Playstation 2 Video Modes (http://users.neoscientists.org/~blue/ps2videomodes.txt)
                                         +                                    
PERFORMING DOUBLE BUFFERING ON THE PS2 (http://www.oopo.net/users/danpeori/releases/ps2-doublebuffer.txt)

 MERGED BY DOCTORXYZ ON 26.05.2009 - VERIFICATION PENDING!!!!!!!!!!!!!!!!!!!
 UPDATED BY DOCTORXYZ ON 28.08.2009 - VERIFICATION PENDING!!!!!!!!!!!!!!!!!!!
 UPDATED BY DOCTORXYZ ON 30.08.2009 - The 0x02(NTSC) and 0x03(PAL) modes does not goes progressive, even forcing them to interlace off and frame mode!
 UPDATED BY DOCTORXYZ ON 22.09.2009 - SMODE2 DOCUMENTATION

---------------------------------------------------------------------------------------------------------
----------------+-------------------+-------------------------+-------+-------+-------+-------+-------|--------------------+
 Mode           | SetGsCrt Mode     | DX                      | DY    | MAGH  | MAGV  | DW    | DH    | "Magic Number"     | 
                |                   | Progressive(Interlaced) |       |       |       |       |       |                    | 
----------------+-------------------+-------------------------+-------+-------+-------+-------+-------|--------------------+
  0: NTSC-NI    | 0x??: NTSC        | 632                     | 25    | 3     | 0     | 2559  | 239   |                    | 
  1: NTSC-I     | 0x02: NTSC        | 632                     | 50    | 3     | 0     | 2559  | 479   | 0x001DF9FF01832278 | 
  2: PAL-NI     | 0x??: PAL         | 652                     | 36    | 3     | 0     | 2559  | 287   |                    | 
  3: PAL-I      | 0x03: PAL         | 652                     | 72    | 3     | 0     | 2559  | 575   | 0x0023F9FF0184828C | 
  4: VESA-1A    | 0x1A: VGA-640@60  | 276 (272)              | 34    | 1     | 0     | 1279  | 479   | 0x001DF4FF00822114 | 
  5: VESA-1C    | 0x1C: VGA-640@75  | 356 (352)               | 18    | 1     | 0     | 1279  | 479   |                    | 
  6: VESA-2B    | 0x2B: VGA-800@60  | 420 (416)               | 26    | 1     | 0     | 1599  | 599   |                    | 
  7: VESA-2D    | 0x2D: VGA-800@75  | 468 (464)               | 23    | 1     | 0     | 1599  | 599   |                    | 
  8: VESA-3B    | 0x3B: VGA-1024@60 | 580 (576)               | 34    | 1     | 0     | 2047  | 767   |                    | 
  9: VESA-3D    | 0x3D: VGA-1024@75 | 260 (256)               | 30    | 0     | 0     | 1023  | 767   |                    | 
 10: VESA-4A    | 0x4A: VGA-1280@60 | 348 (344)               | 40    | 0     | 0     | 1279  | 1023  |                    | 
 11: VESA-4B    | 0x4B: VGA-1280@75 | 348 (376)               | 40    | 0     | 0     | 1279  | 1023  |                    | 
 12: DTV-480P   | 0x50: HDTV-480P   | 232 (228)               | 35    | 1     | 0     | 1439  | 479   | 0x001DF59F008230E8 | 
 13: DTV-1080I  | 0x51: HDTV-1080I  | 238 (234)               | 40    | 0     | 0     | 1919  | 1079  | 0x0043777F000280EE | 
 14: DTV-720P   | 0x52: HDTV-720P   | 302 (298)               | 24    | 0     | 0     | 1279  | 719   |                    | 
----------------+-------------------+-------------------------+-------+-------+-------+-------+-------|--------------------+
Specific Note for HDLoader (splash and main screen):
- Interlace off, Mode 0x1A: VGA-640@60, frame mode
- "Magic Number" is 0x001BF3FF0082815E (calculated by an Excel worksheet)

SMODE2
.----.---.---------.-----------------------------------.
|Name|Pos|Format   |Contents                           |
+----+---+---------+-----------------------------------|
|INT | 0 |int 0:1:0|Interlace Mode Setting             |
|    |   |         |0 Non-Interlace Mode               |
|    |   |         |1 Interlace Mode                   |
|FFMD| 1 |int 0:1:0|Setting in Interlace Mode          |
|    |   |         |0 FIELD Mode(Read every other line)|
|    |   |         |1 FRAME Mode(Read every line)      |
|DPMS|3:2|int 0:2:0|VESA DPMS Mode Setting             |
|    |   |         |00 On          10 Suspend          |
|    |   |         |01 Stand-by    11 Off              |
^----^---^---------^-----------------------------------'

DISPLAY1/DISPLAY2
.----.-----.----------.-------------------------------------.-----.
|Name|Pos. |Format    |Contents                             |Mask |
|    |     |          |                                     |     |
|----+-----+----------+-------------------------------------+-----+
|DX  |11:0 |int 0:12:0|x pos in display area (VCK units)    |0xFFF|
|DY  |22:12|int 0:11:0|y pos in display area (raster units) |0x7FF|
|MAGH|26:23|int 0: 4:0|magnification in horizontal direction|0xF  |
|MAGV|28:27|int 0: 2:0|magnification in vertical direction  |0x3  |
|DW  |43:32|int 0:12:0|display area width - 1 (VCK units)   |0xFFF|
|DH  |54:44|int 0:11:0|display area height - 1 (pixel units)|0x7FF|
'----^-----^----------^-------------------------------------^-----^

Playstation 2 Video Modes (http://users.neoscientists.org/~blue/ps2videomodes.txt)
######################################################################################
CAUTION: Fiddling with the undocumented registers (SMODE1, SYNCH1, SYNCH2
         and SYNCV) might be dangerous. I do NOT take responsibility if your
         monitor goes up in flames :) Use this reference at your own risk!
######################################################################################

SYNCV
.----.-----.----------.---------.
|Name|Pos. |Format    | Contents|
|----+-----+----------+---------|
|VFP | 9:0 |int 0:10:0| ???     |
|VFPE|19:10|int 0:10:0| ???     |
|VBP |31:20|int 0:12:0| ???     |
|VBPE|41:32|int 0:12:0| ???     |
|VDP |52:42|int 0:11:0| ???     |
|VS  |??:53|int 0:??:0| ???     |
'----^-----^----------^---------'

.----------------.-------.-------.-------.-------.-------.-------.
| Mode           | VFP   | VFPE  | VBP   | VBPE  | VDP   | VS    |
|----------------+-------+-------+-------+-------+-------+-------|
|  0: NTSC-NI    |  2    | 6     | 26    | 6     |  480  |  6    |
|  1: NTSC-I     |  1    | 6     | 26    | 6     |  480  |  6    |
|  2: PAL-NI     |  4    | 5     | 33    | 5     |  576  |  5    |
|  3: PAL-I      |  1    | 5     | 33    | 5     |  576  |  5    |
|  4: VESA-1A    | 10    | 0     | 33    | 0     |  480  |  2    |
|  5: VESA-1C    |  1    | 0     | 16    | 0     |  480  |  3    |
|  6: VESA-2B    |  1    | 0     | 23    | 0     |  600  |  4    |
|  7: VESA-2D    |  1    | 0     | 21    | 0     |  600  |  3    |
|  8: VESA-3B    |  3    | 0     | 29    | 0     |  768  |  6    |
|  9: VESA-3D    |  1    | 0     | 28    | 0     |  768  |  3    |
| 10: VESA-4A    |  1    | 0     | 38    | 0     | 1024  |  3    |
| 11: VESA-4B    |  1    | 0     | 38    | 0     | 1024  |  3    |
| 12: DTV-480P   |  6    | 0     | 30    | 0     |  483  |  6    |
| 13: DTV-1080I  |  5    | 0     | 28    | 2     | 1080  | 10    |
| 14: DTV-720P   |  5    | 0     | 20    | 0     |  720  |  5    |
'----------------^-------^-------^-------^-------^-------^-------'

######################################################################################
GS rev.19 or later


*/

/*
SYNCPARAM TABLE

I made it from syncparam at linux/drivers/video/ps2gs.c (inside package http://playstation2-linux.com/files/xrhino-kernel/kernel-source-2.2.1-xr3.tar.bz2)

It would be nice If any of you could help me, finding the relationship dependence between (at least some of) the following fields...!

.-----------------------------------+-------+--------+---------+---------+------------------------------------------------------------------------------------------------------------------------------------------------+--------+-------+-------------------------------|-------------|----------------------------------------------------------|-------------------------------------| 
|                                   |       |        |         |         | smode1                                                                                                                                         |        |       | synch1                        | synch2      | syncv                                                    | display                             | 
|                                   |       |        |         |         |-----.--------.-------.------.--------.------.------.-----.-----.-------.------.------.------.------.------.----.------.------.-------.----.----|        |       |-----.------.------.-----.-----|------.------|----.------.-----.-----.------.---------------------------+-----.------.------.------.----.-----+ 
| Mode                              | width | height | rheight | dvemode | VHP | VCKSEL | SLCK2 | NVCK | CLKSEL | PEVS | PEHS | PVS | PHS | GCONT | SPML | PCK2 | XPCK | SINT | PRST | EX | CMOD | SLCK | T1248 | LC | RC | smode2 | srfsh |  HS | HSVS | HSEQ | HBP | HFP | HB   | HF   |VS  | VDP  | VBPE | VBP | VFPE | VFP |    Magic Number    |DH   | DW   | MAGV | MAGH | DY | DX  | 
|-----------------------------------+-------+--------+---------+---------+-----+--------+-------+------+--------+------+------+-----+-----+-------+------+------+------+------+------+----+------+------+-------+----+----+--------+-------+-----+------+------+-----+-----+------+------+----+------+------+-----+------+-----+--------------------+-----+------+------+------+----+-----+ 
| 00: NTSC-NI   (640x240(224))      |  0640 |   0240 |     224 |       0 |   0 |      1 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    4 |    0 |    0 |    1 |    1 |  0 |    2 |    0 |     1 | 32 |  4 |      0 |     8 | 254 | 1462 |  124 | 222 |  64 | 1652 | 1240 | 06 | 0480 |    6 |  26 |    6 |  02 | 0x00C7800601A01802 |0239 | 2559 |    0 |    3 | 25 | 632 | 
| 01: NTSC-I    (640x480(448))      |  0640 |   0480 |     448 |       0 |   0 |      1 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    4 |    0 |    0 |    1 |    1 |  0 |    2 |    0 |     1 | 32 |  4 |      1 |     8 | 254 | 1462 |  124 | 222 |  64 | 1652 | 1240 | 06 | 0480 |    6 |  26 |    6 |  01 | 0x00C7800601A01801 |0479 | 2559 |    0 |    3 | 50 | 632 | 
| 02: PAL-NI    (640x288(256))      |  0640 |   0288 |     256 |       1 |   0 |      1 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    4 |    0 |    0 |    1 |    1 |  0 |    3 |    0 |     1 | 32 |  4 |      0 |     8 | 254 | 1474 |  127 | 262 |  48 | 1680 | 1212 | 05 | 0576 |    5 |  33 |    5 |  04 | 0x00A9000502101404 |0287 | 2559 |    0 |    3 | 36 | 652 | 
| 03: PAL-I     (640x576(512))      |  0640 |   0576 |     512 |       1 |   0 |      1 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    4 |    0 |    0 |    1 |    1 |  0 |    3 |    0 |     1 | 32 |  4 |      1 |     8 | 254 | 1474 |  127 | 262 |  48 | 1680 | 1212 | 05 | 0576 |    5 |  33 |    5 |  01 | 0x00A9000502101401 |0575 | 2559 |    0 |    3 | 72 | 652 | 
| 04: VESA-1A   (640x480 59.940Hz)  |  0640 |   0480 |      -1 |       2 |   1 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    2 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     1 | 15 |  2 |      0 |     4 | 192 | 0608 |  192 | 084 |  32 | 0768 | 0524 | 02 | 0480 |    0 |  33 |    0 |  10 | 0x004780000210000A |0479 | 1279 |    0 |    1 | 34 | 276 | 
| 05: VESA-1C   (640x480 75.000Hz)  |  0640 |   0480 |      -1 |       2 |   1 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    2 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     1 | 28 |  3 |      0 |     4 | 128 | 0712 |  128 | 228 |  32 | 0808 | 0484 | 03 | 0480 |    0 |  16 |    0 |  01 | 0x0067800001000001 |0479 | 1279 |    0 |    1 | 18 | 356 | 
| 06: VESA-2B   (800x600 60.317Hz)  |  0800 |   0600 |      -1 |       2 |   1 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    2 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     1 | 71 |  6 |      0 |     4 | 256 | 0800 |  256 | 164 |  80 | 0976 | 0636 | 04 | 0600 |    0 |  23 |    0 |  01 | 0x0089600001700001 |0599 | 1599 |    0 |    1 | 26 | 420 | 
| 07: VESA-2D   (800x600 75.000Hz)  |  0800 |   0600 |      -1 |       2 |   1 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    2 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     1 | 44 |  3 |      0 |     4 | 160 | 0896 |  160 | 308 |  32 | 1024 | 0588 | 03 | 0600 |    0 |  21 |    0 |  01 | 0x0069600001500001 |0599 | 1599 |    0 |    1 | 23 | 468 | 
| 08: VESA-3B   (1024x768 60.004Hz) |  1024 |   0768 |      -1 |       2 |   1 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    2 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     0 | 58 |  6 |      0 |     4 | 272 | 1072 |  272 | 308 |  48 | 1296 | 0764 | 06 | 0768 |    0 |  29 |    0 |  03 | 0x00CC000001D00003 |0767 | 2047 |    0 |    1 | 34 | 580 | 
| 09: VESA-3D   (1024x768 75.029Hz) |  1024 |   0768 |      -1 |       2 |   1 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    1 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     1 | 35 |  3 |      0 |     2 | 096 | 0560 |  096 | 164 |  16 | 0640 | 0396 | 03 | 0768 |    0 |  28 |    0 |  01 | 0x006C000001C00001 |0767 | 1023 |    0 |    0 | 30 | 260 | 
| 10: VESA-4A   (1280x1024 60.020Hz)|  1280 |   1024 |      -1 |       2 |   1 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    1 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     0 | 08 |  1 |      0 |     2 | 112 | 0732 |  112 | 236 |  16 | 0828 | 0496 | 03 | 1024 |    0 |  38 |    0 |  01 | 0x0070000002600001 |1023 | 1279 |    0 |    0 | 40 | 348 | 
| 11: VESA-4B   (1280x1024 75.025Hz)|  1280 |   1024 |      -1 |       2 |   1 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    1 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     0 | 10 |  1 |      0 |     2 | 144 | 0700 |  144 | 236 |  16 | 0828 | 0464 | 03 | 1024 |    0 |  38 |    0 |  01 | 0x0070000002600001 |1023 | 1279 |    0 |    0 | 40 | 380 | 
| 12: DTV-480P  (720x480)           |  0720 |   0480 |      -1 |       3 |   1 |      1 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    2 |    0 |    0 |    1 |    1 |  0 |    0 |    0 |     1 | 32 |  4 |      0 |     4 | 128 | 0730 |  128 | 104 |  32 | 0826 | 0626 | 06 | 0483 |    0 |  30 |    0 |  06 | 0x00C78C0001E00006 |0479 | 1439 |    0 |    1 | 35 | 232 | 
| 13: DTV-1080I (1920x1080)         |  1920 |   1080 |      -1 |       4 |   0 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    1 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     1 | 22 |  2 |      1 |     4 | 104 | 1056 |  044 | 134 |  30 | 1064 | 0868 | 10 | 1080 |    2 |  28 |    0 |  05 | 0x0150E00201C00005 |1079 | 1919 |    0 |    0 | 40 | 238 | 
| 14: DTV-720P  (1280x720)          |  1280 |   0720 |      -1 |       5 |   1 |      0 |     1 |    1 |      1 |    0 |    0 |   0 |   0 |     0 |    1 |    0 |    0 |    1 |    0 |  0 |    0 |    0 |     1 | 22 |  2 |      0 |     4 | 104 | 0785 |  040 | 198 |  62 | 0763 | 0529 | 05 | 0720 |    0 |  20 |    0 |  05 | 0x00AB400001400005 |0719 | 1279 |    0 |    0 | 24 | 302 | 
'-----------------------------------^-------^--------^---------^---------^-----^--------^-------^------^--------^------^------^-----^-----^-------^------^------^------^------^------^----^------^------^-------^----^----^--------^-------^-----^------^------^-----^-----^------^------^----^------^------^-----^------^-----^--------------------^------^-----^------^------^----^-----' 
*/

/*
ASM Directives - From "MIPS/SPIM Reference Card" (c) 2007 Jan Wtzig, Staatliche Studienakademie Dresden (www.ba-dresden.de/~jan)
---------------------------------------------------------------------------------------------------------------------------------
Name					Description
----					-----------
.data [addr]*			Subsequent items are stored in the data segment 
.kdata [addr]*			Subsequent items are stored in the kernel data segment 
.ktext [addr]*			Subsequent items are stored in the kernel text segment 
.text [addr]*			Subsequent items are stored in the text 
Note: *starting at [addr] if specified

.ascii str				Store string str in memory, but do not null-terminate it 
.asciiz str				Store string str in memory and null-terminate it 
.byte b1, . . . , bn	Store the n values in successive bytes of memory 
.double d1, . . . , dn	Store the n floating-point double precision numbers in successive memory locations
.float f1, . . . , f1	Store the n floating-point single precision numbers in successive memory locations 
.half h1, . . . , hn	Store the n 16-bit quantities in successive memory halfwords 
.word w1, . . . ,wn		Store the n 32-bit quantities in successive memory words 
.space n				Allocate n bytes of space in the current segment 

.extern symsize			Declare that the datum stored at sym is size bytes large and is a global label 
.globl sym				Declare that label sym is global and can be referenced from other files 

.align n				Align the next datum on a 2n byte boundary, until the next .data or .kdata directive 
.set at					Tells SPIM to complain if subsequent instructions use $at 
.set noat				Prevents SPIM from complaining if subsequent instructions use $at 
*/

/* 
Data Address Breakpointing
--------------------------
The following explanations and related example is based (but not tested yet!) on my understanding of following documents
- "TX System RISC TX79 Core Architecture (Symmetric 2-way superscalar 64-bit CPU) Rev. 2.0" - Toshiba Corporation (http://lukasz.dk/files/tx79architecture.pdf.)
   The TX79 is very similar to the EE, and example code into this document is very close to the setDataAddressWriteBreakpoint function from PS2 Zone Loader.
- "EE Core Instruction Set Manual" - Sony PS2 SDK
- "MIPS Architecture and Assembly Language Overview" (http://logos.cs.uic.edu/366/notes/MIPS%20Quick%20Tutorial.htm)

--x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-

Sequence of Setting Breakpoint
------------------------------
1. Synchronize the pipeline -> SYNC.L
2. Disable the breakpoint exception that is going to be reconfigured -> BED = 1
3. Synchronize the pipeline -> SYNC.P
4. Set appropriate data in Breakpoint register pairs ->  DAB = 0x12000080 and DABM = 0xFFFFFFDF
5. Set appropriate configuration into Breakpoint Control Register, including enabling the break point exception. -> DWE =1, DUE = 1 and BED = 0
6. Synchronize the pipeline -> SYNC.P

--x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-

The following code sets a data address breakpoint for both 0x1200_0080 and 0x1200_00A0 for
writing, and traps if the processor is in user mode.
------------------------------------------------------------------
#
# Setting data address breakpoint from 0x1200_0080 and 0x1200_00A0 in user mode
#

sync.l				# 1st sync.
					# A barrier to ensure there is no pending data address breakpoint in pipe.
					# Must flush all buffers for load/store for this purpose by SYNC.L

# At first, reset data-breakpoint related bits to zeros.
# Resetting DWB/DRB is important so that the handler can recognize the next breakpoint exception correctly.
mfbpc $a0			# Move From Breakpoint Control Register (BPC). Load current configuration from it.
li $a1, ~( \
( 1 << 30 ) 		# DRE \
| ( 1 << 29 ) 		# DWE \
| ( 1 << 28 ) 		# DVE \
| ( 1 << 21 ) 		# DUE \
| ( 1 << 20 ) 		# DSE \
| ( 1 << 19 ) 		# DKE \
| ( 1 << 18 ) 		# DXE \
| ( 1 << 16 ) 		# DTE \
| ( 1 << 2 ) 		# DWB \
| ( 1 << 1 ) 		# DRB \
)
and $a0, $a0, $a1
mtbpc $a0			# Move to Breakpoint Control Register. Reload it.

sync.p				# 2nd sync.
					# barrier to ensure the configuration change of breakpoint function

li $a2, 0x12000080	# Reconfigure Data Address Breakpoint Register (DAB)
					# Note that the bit 61 can be anything because it is masked by DABM register anyway
mtdab $a2			# Move to Data Address Breakpoint Register (DAB)

li $a1, 0xFFFFFFDF	# Setting Data Address Breakpoint Mask Register (DABM)
					# Masked if corresponding bit in mask register is reset to zero
mtdabm $a1			# Move to Data Address Breakpoint Mask Register (DABM)

# Set new configuration to BPC register.
# Note that setting BPC after DAB/DABM is so important to avoid spurious exception.
li $a2, $a2, \
( \
| ( 1 << 29 )		# DWE = 1 to enable Data B.P on write \
| ( 1 << 21 )		# DUE = 1 to enable Data B.P in user mode. \
| ( 1 << 15 )		# BED = 1 to enable generating exception. \
)
or $a1, $a0, $a2	# Note that $a0 still holds the value used on MTBPC.
mtbpc $a1

sync.p				# 3rd sync.
					# Barrier to ensure the configuration change

--x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-

Breakpoint Control Register (BPC)
---------------------------------
This register consists of 5 parts of bit fields:
 Breakpoint overall control (bit [31:28])
These bits controls the operation mode of the breakpointing.
 Instruction breakpoint control (bit [26:23])
These bits specifies the processor mode that the instruction breakpoint is enabled.
 Data breakpoint control (bit[21:18])
These bits specifies the processor mode that the data breakpoint is enabled.
 Signaling Control (bit[17:15])
These bits controls the occurrence of breakpoint exception / trigger generation upon the breakpoint event.
 Breakpoint Status (bit[2:0])
These bits indicates the type of breakpoint event. This part is used to identify which breakpoint event occurred in the breakpoint exception handler.

BPC Register Fields
-------------------
Field Bits Description                                                        Type      Initial Value
----- ---- -----------                                                        ----      -------------
BED   15   Breakpoint Exception Disable. This bit disables the entry into     Read /    Undefined
           the debug exception handler. Note that the setting of this bit     Write              
           does not affect trigger signal generation.
           0: enable entry into debug exception handler
           1: disable entry into debug exception handler

DWE   29   Data Write Enable. This bit enables data store address             Read /    0
           breakpointing.                                                     Write              
           0: disable breakpointing on writes
           1: enable breakpointing on writes

DUE   21   Data break - User Enable. This bit enables data breakpointing in   Read /    Undefined
           User mode. This bit is only valid if DWE or DRE is set to 1.       Write    
           0: disable data breakpointing in User mode                            
           1: enable data breakpointing in User mode
*/

/*
doctorxyz's PS2 SCPH 30001/Matrix Infinity v1.93
+-----------+------------+-------------+------------+---------------------------+
| Scenario  | Syscall    | sceSetGsCrt | Core Debug | Installers for            | 
|           | Vector     |             | Exception  +-------------+-------------| 
|           | Table      |             | Handler    | sceSetGsCrt |Core Debug   | 
|           |            |             |            |             |Excep.Handler| 
+-----------+------------+-------------+------------+-------------+-------------+
|MI VGA OFF | 0x80014E80 | 0x8000BA08  | 0x000802C0 | 0x????????  | 0x????????  |
|MI VGA ON  | 0x80014E80 | 0x800531D8  | 0x80050950 | 0x????????  | 0x????????  |
|MI OFF     | 0x???????? | 0x????????  | 0x???????? | 0x????????  | 0x????????  |
+-----------+------------+-------------+------------+-------------+-------------+


katananja's PS2 SCPH-90006 Ceramic White - HK Thunder Pro II Gold
+-----------+------------+-------------+------------+---------------------------+
| Scenario  | Syscall    | sceSetGsCrt | Core Debug | Installers for            | 
|           | Vector     |             | Exception  +-------------+-------------| 
|           | Table      |             | Handler    | sceSetGsCrt |Core Debug   | 
|           |            |             |            |             |Excep.Handler| 
+-----------+------------+-------------+------------+-------------+-------------+
|off        | 0x???????? | 0x????????  | 0x???????? | 0x????????  | 0x????????  |
|480p       | 0x80015500 | 0x8000C3D0  | 0x8007B200 | 0x????????  | 0x????????  |
|720p       | 0x???????? | 0x????????  | 0x???????? | 0x????????  | 0x????????  |
+-----------+------------+-------------+------------+-------------+-------------+


*/
